/**
 * Created by Administrator on 2015/5/29.
 */

//------------------------------------------------------------------------------------------------------------------

function Article() {

    this._id = '';          //数据库id
    this.title = '';        //标题
    this.abstract = '';     //简介
    this.content = '';      //内容
    this.insertTime = '';   //发布时间
    this.category = '';     //分类

}

Article.getById = function (_id, successCallback, errorCallback) {

    $.ajax({
        url: '/articles/content',
        type: 'get',
        data: {_id: _id},
        dataType: 'json',
        success: successCallback,
        error: errorCallback
    })
}

//------------------------------------------------------------------------------------------------------------------
//获取文章列表
Article.list = function (page, successCallback, errorCallback) {

    $.ajax({
        url: '/articles/list',
        type: 'get',
        data: page,
        dataType: 'json',
        success: successCallback,
        error: errorCallback
    })
}

//------------------------------------------------------------------------------------------------------------------
Article.delete = function (idList, successCallback, errorCallback) {

    $.ajax({
        url: '/admin/articles/content/',
        type: 'delete',
        data: {_idList: idList},
        dataType: 'json',
        success: successCallback,
        error: errorCallback
    })

}

//------------------------------------------------------------------------------------------------------------------
//保存文章
Article.prototype.save = function (callback) {
    var _this = this;
    var article = {
        _id: _this._id,
        title: _this.title,
        abstract: _this.abstract,
        content: _this.content,
        insertTime: _this.insertTime,
        category: _this.category
    }

    //如果article._id为空，则为创建(POST)，否则为更新(PUT)
    var ajaxType = (!article._id || '' == article._id) ? 'post' : 'put';
    $.ajax({
        url: '/admin/articles/content/',
        type: ajaxType,
        data: {article: article},
        dataType: 'json',
        success: function (data) {
            alert('保存成功') ;
            callback();
        },
        error: function (data) {
            alert('保存失败');
            callback();
        }
    });
}
//------------------------------------------------------------------------------------------------------------------


